package atomic;

import java.util.concurrent.CountDownLatch;

/**
 * volatile测试：我能想到最好的测试方法
 * 因为无法避免非原子性操作
 * 
 * @author lcx
 *
 */
public class VolatileValidate {

	static volatile int count=0;

	public static void main(String[] args) {

		CountDownLatch latch=new CountDownLatch(2);
		new volThread(latch).start();
		new volThread(latch).start();
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("count="+count);
	}
}

class volThread extends Thread
{
	CountDownLatch latch;
	public volThread(CountDownLatch latch)
	{
		this.latch=latch;
	}
	
	public void run()
	{
		for(int i=0;i<10000;i++)
			VolatileValidate.count++;
		latch.countDown();
	}
}
